package com.bsth.repository.schedule;

import com.bsth.entity.schedule.CarConfigInfo;
import com.bsth.entity.schedule.EmployeeConfigInfo;
import com.bsth.repository.BaseRepository;

import java.util.List;
import java.util.Map;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

/**
 * Created by xu on 16/5/10.
 */
@Repository
public interface EmployeeConfigInfoRepository extends BaseRepository<EmployeeConfigInfo, Long> {
	
	@EntityGraph(value = "employeeConfigInfo_jsy_spy_xl", type = EntityGraph.EntityGraphType.FETCH)
	@Query("select ec from EmployeeConfigInfo ec where ec.xl.lineCode=?1")
	List<EmployeeConfigInfo> findBylineCode(String lineCode);

    @EntityGraph(value = "employeeConfigInfo_jsy_spy_xl", type = EntityGraph.EntityGraphType.FETCH)
    List<EmployeeConfigInfo> findByXlId(Integer xlid);

//    @EntityGraph(value = "employeeConfigInfo_jsy_spy_xl", type = EntityGraph.EntityGraphType.FETCH)
    // 因为使用了in操作符，用 @EntityGraph会报错，升到hibernate 5.0.4就好了，暂时不用@EntityGraph
    // Hibernate ORMHHH-9230
    @Override
    Page<EmployeeConfigInfo> findAll(Specification<EmployeeConfigInfo> spec, Pageable pageable);

    @EntityGraph(value = "employeeConfigInfo_jsy_spy_xl", type = EntityGraph.EntityGraphType.FETCH)
    @Override
    List<EmployeeConfigInfo> findAll(Specification<EmployeeConfigInfo> spec);

    @EntityGraph(value = "employeeConfigInfo_jsy_spy_xl", type = EntityGraph.EntityGraphType.FETCH)
    @Query("select cc from EmployeeConfigInfo cc where cc.id=?1")
    EmployeeConfigInfo findOneExtend(Long aLong);

    @Query("select new map(" +
            "ec.jsy.id as jsyId, " +
            "ec.jsy.jobCode as jsyGh, " +
            "ec.jsy.personnelName as jsyName) " +
            "from EmployeeConfigInfo ec ")
    List<Map<String, Object>> findJsyFromConfig();

    @Query("select new map(" +
            "ec.spy.id as spyId, " +
            "ec.spy.jobCode as spyGh, " +
            "ec.spy.personnelName as spyName) " +
            "from EmployeeConfigInfo ec ")
    List<Map<String, Object>> findSpyFromConfig();
}
